Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Configure sentry in external config #12681

Merged
merged 10 commits into from
Jul 1, 2024
Merged

Conversation

s1gr1d
Copy link
Member

@s1gr1d s1gr1d commented Jun 27, 2024

To be able to differentiate between a browser/client execution context, sentry is initialized in an external config file. An import statement in nuxt-root.vue is added which loads this config file.

Nuxt tracking issue: #9095

"import": "./build/esm/index.client.js",
"require": "./build/cjs/index.client.js"
},
"node": "./build/cjs/index.server.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: this should be both esm and cjs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually I'd double check this too to see if ESM works well on the server 😅

packages/nuxt/README.md Show resolved Hide resolved
}
},
});

function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined {
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts'];
const possibleFileExtensions = ['ts', 'js', 'mjs', 'cjs', 'mts', 'cts'];

if (resolver) {
addPlugin(resolver.resolve('runtime/plugins/sentry.client.js'));
addPlugin(resolver.resolve('./runtime/plugins/sentry.client'));
addServerPlugin(resolver.resolve('./runtime/plugins/sentry.server'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: It seems like this PR has some server-side logic with the addServerPlugin and the server side sdk being exposed.

To ease reviewing, could we remove these changes from this PR and add it later when we look at the server-side stuff?

addImportStatement(nuxtApp.rootComponent, buildSdkInitFileImportSnippet(pathToClientInit));
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: we should add some notes that this is coming from sentry and we encountered this error while trying to add imports.

defaults: {},
setup(_moduleOptions, _nuxt) {
setup(_moduleOptions, nuxt) {
const resolver: Resolver = createResolver(import.meta.url);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Can we double check to see if this works with CJS? How does this get transpiled? We currently have build/module/module.cjs emitted.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the module, I am using the nuxt module builder which transpiles the files exactly how they should be imported in Nuxt. The module.cjs looks like this:

module.exports = function(...args) {
  return import('./module.mjs').then(m => m.default.call(this, ...args))
}
const _meta = module.exports.meta = require('./module.json')
module.exports.getMeta = () => Promise.resolve(_meta)

@@ -1,3 +1,3 @@
import type { init } from '@sentry/vue';

export type SentryVueOptions = Parameters<typeof init>[0] & object;
export type SentryVueOptions = Omit<Parameters<typeof init>[0] & object, 'app'>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Would be useful to leave a comment about why this we have a difference between vue init and nuxt init here.

/** Returns an import snippet */
export function buildSdkInitFileImportSnippet(filePath: string): string {
const pathToPosix = (originalPath: string): string => {
return originalPath.split(path.sep).join(path.posix.sep);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: why elect for the inline function pathToPosix over

const posixPath = filePath.split(path.sep).join(path.posix.sep);

return `import "${posixPath}"`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True 😅 I had two functions first and moved pathToPosix inside the other one.

fs.writeFileSync(filePath, output, 'utf8');
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Error writing file: ${err}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Can we indicate that this comes from sentry? Same with console.error below.

packages/nuxt/src/common/snippets.ts Show resolved Hide resolved
@s1gr1d s1gr1d requested a review from AbhiPrasad June 28, 2024 13:07
Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing all my comments!

Only have some things about addImportStatement left, but don't let that block the merge.

packages/nuxt/README.md Show resolved Hide resolved
export function addImportStatement(filePath: string, importStatement: string): void {
try {
const data = fs.readFileSync(filePath, 'utf8');
const scriptIndex = data.indexOf('<script setup>');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: Let's extract <script setup> into a constant, and add a comment about why we are looking for this.

const scriptIndex = data.indexOf('<script setup>');

// Insert the import statement after the script tag
const output = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: I think doing string.replace is a bit easier to understand then indexOf + constructing a new output via slice

const output = data.replace(SETUP_TAG, `${SETUP_TAG}\n${importStatement}`);

}
} catch (err) {
// eslint-disable-next-line no-console
console.error(`[Sentry] Error reading file at ${filePath}: ${err}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: we should have emit a error message if we fail to find <script setup> in the read file, and make this separate from the error message we get from fs.readFileSync failing.


export default makeNPMConfigVariants(
makeBaseNPMConfig({
entrypoints: ['src/index.client.ts', 'src/client/index.ts'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need both of these entry points? What's the difference between them?

@@ -1 +1 @@
export {};
export * from './client';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: This will also have to export the types from the server, but we can do that in a follow up :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far there is no server-side instrumentation. But when this gets added I will also add the types here 👍🏻

},
"./package.json": "./package.json"
"./module": {
"types": "./build/module/types.d.ts",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Will we have to do the same as for solid here, where types for a submodule export have to be in the root? 😬 no need to adjust this in this PR, but have a look at it in a follow up, and maybe sync with @andreiborza on this :)

Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some comments about follow up stuff we should not forget, but looks good to be merged to me 👍

@s1gr1d s1gr1d merged commit f60aae5 into develop Jul 1, 2024
83 checks passed
@s1gr1d s1gr1d deleted the sig/nuxt-external-init branch July 1, 2024 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants